<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="JavaScript" type="text/JavaScript">
//ajax base
function createRequest() {
  var request = null;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }

  if (request == null) {
    alert("Error creating request object!");
  } else {
    return request;
  }
}

function addPoints() {
	document.getElementById("saveButton").disabled = true;
	document.getElementById("ajaxStatus").innerHTML = "Posting Points...";
	var driver = document.getElementById("seniority_driver").options[document.getElementById("seniority_driver").selectedIndex].value;
	var points = document.getElementById("seniority_points").value;
	var reason = document.getElementById("seniority_reason").value;
	var url = 'includes/ajax.php?points=post&driver=' + driver + '&numpoints=' + points + '&reason=' + reason;
	alert(url);
	
	http_request = createRequest();
	http_request.onreadystatechange = function(){results(http_request);};
	http_request.open('GET', url, true);
	http_request.send(null);
	
}

function results (requestObj) {
	if (requestObj.readyState == 4) {
		if (requestObj.status == 200) {
			responseXML = requestObj.responseXML.documentElement;
			var responseError = responseXML.getElementsByTagName('error')[0].firstChild.data;
			var responseMsg = responseXML.getElementsByTagName('msg')[0].firstChild.data;
			var responseTotal = responseXML.getElementsByTagName('total')[0].firstChild.data;
			var points = document.getElementById("seniority_points").value;
			if(points > 0) points = '+' + points;
			if(responseError == 0) {
				var selectedDriver = document.getElementById("seniority_driver").options[document.getElementById("seniority_driver").selectedIndex].text;
				insertOption(selectedDriver + ": " + points + "(" + responseTotal + ")",document.getElementById("seniority_completed"));
				if(document.getElementById("clearFields").checked === true) {
					document.getElementById("seniority_points").value = '';
					document.getElementById("seniority_reason").value = '';
					document.getElementById("seniority_driver").selectedIndex = 0;
				}
			} else {
				alert(responseMsg);
			}
			document.getElementById("ajaxStatus").innerHTML = "";
			document.getElementById("saveButton").disabled = false;
		}
	}
}

function insertOption(addText,x) {
	var y=document.createElement('option');
	y.text=addText
	try {
		x.add(y,null); // standards compliant
	}
	catch(ex) {
		x.add(y); // IE only
	}
}

</script>
</head>

<body style="text-align:center; ">
<h2>Modify Seniority Points</h2>
<div id="ajaxStatus" style="height:20px; font-weight:bold; text-align:center; "></div>
<table style="width: 350px; margin:auto;">
  <tr>
    <td>Driver </td>
    <td><select name="select" style="width: 200px; " id="seniority_driver">
      <option value="1">Neilson, Matt</option>
      <option value="2">Lapointe, Ryan</option>
    </select></td>
  </tr>
  <tr>
    <td>Points </td>
	<td><input name="text" id="seniority_points" type="text" style="width:75px;" /></td>

  </tr>
  <tr>
    <td>Reason for Adjustment:</td>
    <td ><textarea style="width: 200px; " id="seniority_reason"></textarea></td>
  </tr>
  <tr>
  </tr>
  <tr>
    <td colspan="2" style="text-align:center; "><input type="checkbox" id="clearFields" value="1" > 
    On save, reset fields</td>
  </tr>
  <tr>
    <td colspan="2" style="text-align:center; "><input type="button" id="saveButton" value="Save" onclick="addPoints()"></td>
  </tr>
  <tr>
    <td colspan="2" style="text-align:center; "><select multiple size="10" style="width:298px; " id="seniority_completed"><option>Neilson,Matthew: +35.25</option></select></td>
  </tr>
</table>


</body>
</html>
